home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / LabelButton.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  17.6 KB  |  644 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.FontMetrics;
  7. import java.awt.Font;
  8. import java.awt.Graphics;
  9. import java.awt.Rectangle;
  10.  
  11. //     01/29/97    TWB    Integrated changes from Macintosh
  12. //     02/27/97    RKM    Merged in Scott's changes
  13.  
  14. /**
  15.  * Creates a text string that initiates an action when clicked. A LabelButton 
  16.  * appears outlined when it possesses input focus. This indicates that the 
  17.  * component can receive a user event. When the user clicks the button, it 
  18.  * appears depressed to indicate that it has detected the click event. 
  19.  * <p>
  20.  * When a LabelButton is disabled, it is ægrayed out.Æ  This indicates that 
  21.  * it can not receive user input.
  22.  * <p>
  23.  * The text string accepts clicked events automatically by default.
  24.  * <p>
  25.  * Use LabelButton to:
  26.  * <UL>
  27.  * <DT>╖ Accept or yield focus.
  28.  * <DT>╖ Respond to a user event.
  29.  * <DT>╖ Send a train of action events to another component. 
  30.  * <p>
  31.  * To define an interaction with another component, use the Interaction Wizard. 
  32.  * <p>
  33.  * @version 1.0, Nov 26, 1996
  34.  * @author  Symantec
  35.  */
  36.  
  37. public class LabelButton
  38.     extends ButtonBase
  39.     implements AlignStyle,
  40.                BevelStyle
  41. {
  42.     //--------------------------------------------------
  43.     // constants
  44.     //--------------------------------------------------
  45.  
  46.     /**
  47.      * Constant indicating a drawing margin of 0 pixels around the inside of 
  48.      * the border.
  49.      */
  50.     public static final int INDENT_ZERO = 0;
  51.  
  52.     /**
  53.      * Constant indicating a drawing margin of 1 pixel around the inside of 
  54.      * the border.
  55.      */
  56.     public static final int INDENT_ONE = 1;
  57.  
  58.     /**
  59.      * Constant indicating a drawing margin of 2 pixels around the inside of 
  60.      * the border.
  61.      */
  62.     public static final int INDENT_TWO = 2;
  63.  
  64.  
  65.  
  66.     //--------------------------------------------------
  67.     // class variables
  68.     //--------------------------------------------------
  69.  
  70.  
  71.     //--------------------------------------------------
  72.     // member variables
  73.     //--------------------------------------------------
  74.  
  75.     /**
  76.      * The button label text.
  77.      */
  78.     protected String sLabelButton;
  79.     /**
  80.      * Indicates whether to display the label text.
  81.      */
  82.     protected boolean showLabel;
  83.     private int alignStyle;
  84.     private int bevelStyle;
  85.     private Color color1;
  86.     private Color color2;
  87.     private Color textColor;
  88.     private Color borderedColor;
  89.     private FontMetrics fm;
  90.     private int xTemp;
  91.     private int yTemp;
  92.     private int indent = INDENT_ZERO;
  93.     private int textWidth;
  94.     private int textAscent;
  95.     private int textHeight;
  96.     private boolean bOsFlag = false;
  97.  
  98.  
  99.  
  100.     //--------------------------------------------------
  101.     // constructors
  102.     //--------------------------------------------------
  103.  
  104.     /**
  105.      * Constructs a LabelButton with black text, center aligned, raised border, 
  106.      * no text, and a zero border indent.
  107.      */
  108.     public LabelButton()
  109.     {
  110.         this("", ALIGN_CENTERED, BEVEL_RAISED, Color.black, INDENT_ZERO, true);
  111.     }
  112.  
  113.     /**
  114.      * Constructs a LabelButton with black text, ZERO indent
  115.      * @param sText text of the LabelButton
  116.      * @param style numeric value indicating LabelButton style, such as LabelButton.IN + LabelButton.CENTERED
  117.      */
  118.     /**
  119.      * Constructs a LabelButton with black text, a zero border indent, and the 
  120.      * specified text alignment, and bevel styles.
  121.      * @param sText text of the LabelButton
  122.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  123.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  124.      */
  125.     public LabelButton(String sText, int alignStyle, int bevelStyle)
  126.     {
  127.         this(sText, alignStyle, bevelStyle, Color.black, INDENT_ZERO, true);
  128.     }
  129.  
  130.     /**
  131.      * Constructs a LabelButton with a zero border indent and the specified attributes.
  132.      * @param sText text of the LabelButton
  133.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  134.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  135.      * @param color the label text color
  136.      */
  137.     public LabelButton(String sText, int alignStyle, int bevelStyle, Color color)
  138.     {
  139.         this(sText, alignStyle, bevelStyle, color, INDENT_ZERO, true);
  140.     }
  141.  
  142.     /**
  143.      * Constructs a Label3D with black label text and the specified attributes.
  144.      * @param sText text of the LabelButton
  145.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  146.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  147.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  148.      * INDENT_ONE,  or INDENT_TWO
  149.      */
  150.     public LabelButton(String sText, int alignStyle, int bevelStyle, int indent)
  151.     {
  152.         this(sText, alignStyle, bevelStyle, Color.black, indent, true);
  153.     }
  154.  
  155.     /**
  156.      * Constructs a Label3D with the specified attributes.
  157.      * @param sText text of the Label3D
  158.      * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  159.      * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  160.      * @param color the label text color
  161.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  162.      * INDENT_ONE,  or INDENT_TWO
  163.      * @param f show the label text if true
  164.      */
  165.     public LabelButton(String sText, int alignStyle, int bevelStyle, Color color, int indent, boolean f)
  166.     {
  167.         String sOs = System.getProperty("os.name");
  168.  
  169.         if (sOs.startsWith("S") ||      // SunOS, Solaris
  170.             sOs.startsWith("OSF") )     // OSF
  171.         {
  172.             bOsFlag = true;
  173.             setFont(new Font("Dialog", Font.PLAIN, 10));
  174.         }
  175.         else
  176.         {
  177.             bOsFlag = false;
  178.         }
  179.  
  180.         Font tempFont = getFont();
  181.         if(tempFont == null)
  182.         {
  183.             fm = getFontMetrics(new Font("Dialog", Font.PLAIN, 12));
  184.         }
  185.         else
  186.         {
  187.             fm = getFontMetrics(tempFont);
  188.         }
  189.         
  190.         setText(sText);
  191.         textColor = color;
  192.         borderedColor = Color.black;
  193.  
  194.         setBorderIndent(indent);
  195.         setAlignStyle(alignStyle);
  196.         setBevelStyle(bevelStyle);
  197.         showFocus = true;
  198.         showLabel = f;
  199.     }
  200.  
  201.     //--------------------------------------------------
  202.     // accessor methods
  203.     //--------------------------------------------------
  204.  
  205.     /**
  206.      * Sets the alignment style.
  207.      * @param style text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  208.      * @see #getAlignStyle
  209.      */
  210.     public void setAlignStyle(int style)
  211.     {
  212.         alignStyle = style;
  213.     }
  214.  
  215.     /**
  216.      * Gets the current alignment style.
  217.      * @return text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
  218.      * @see #setAlignStyle
  219.      */
  220.     public int getAlignStyle()
  221.     {
  222.         return (alignStyle);
  223.     }
  224.  
  225.     /**
  226.      * Sets the border style.
  227.      * @param style border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  228.      * @see #getBevelStyle
  229.      */
  230.     public void setBevelStyle(int style)
  231.     {
  232.         bevelStyle = style;
  233.  
  234.         switch(style)
  235.         {
  236.             case BEVEL_LOWERED :
  237.             {
  238.                 color1 = Color.black;
  239.                 color2 = Color.gray;
  240.                 break;
  241.             }
  242.             case BEVEL_RAISED :
  243.             {
  244.                 color1 = Color.gray;
  245.                 color2 = Color.black;
  246.                 break;
  247.             }
  248.             case BEVEL_LINE :
  249.             {
  250.                 color1 = borderedColor;
  251.                 color2 = borderedColor;
  252.                 break;
  253.             }
  254.             default: // catches any bogus type, paint(...) relies on color1 being null
  255.             {
  256.                 color1 = color2 = null;
  257.                 break;
  258.             }
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * Gets the current border style.
  264.      * @return border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
  265.      * @see #setBevelStyle
  266.      */
  267.     public int getBevelStyle()
  268.     {
  269.         return (bevelStyle);
  270.     }
  271.  
  272.     /**
  273.      * Sets the border indent amount.
  274.      * @param indent the amount to indent around the border: INDENT_ZERO, 
  275.      * INDENT_ONE,  or INDENT_TWO
  276.      * @see #getBorderIndent
  277.      */
  278.     public void setBorderIndent(int indent)
  279.     {
  280.         if (indent < INDENT_ZERO)
  281.         {
  282.             this.indent = INDENT_ZERO;
  283.         }
  284.         else if (indent > INDENT_TWO)
  285.         {
  286.             this.indent = INDENT_TWO;
  287.         }
  288.         else
  289.         {
  290.             this.indent = indent;
  291.         }
  292.     }
  293.  
  294.     /**
  295.      * Gets the current border indent amount.
  296.      * @return the amount currently indented around the border: INDENT_ZERO, 
  297.      * INDENT_ONE,  or INDENT_TWO
  298.      * @see #setBorderIndent
  299.      */
  300.     public int getBorderIndent()
  301.     {
  302.         return indent;
  303.     }
  304.  
  305.     /**
  306.      * Sets the border color.
  307.      * @param color color to use for the border
  308.      */
  309.     public void setBorderedColor(Color color)
  310.     {
  311.         borderedColor = color;
  312.  
  313.         if (bevelStyle == BEVEL_LINE)
  314.         {
  315.             color1 = color;
  316.             color2 = color;
  317.         }
  318.  
  319.         invalidate();
  320.     }
  321.  
  322.     /**
  323.      * Sets the label text.
  324.      * @param sLabel the new label text
  325.      * @see #getText
  326.      */
  327.     public void setText(String sText)
  328.     {
  329.         Font tempFont = getFont();
  330.         if(tempFont == null)
  331.         {
  332.             fm = getFontMetrics(new Font("Dialog", Font.PLAIN, 12));
  333.         }
  334.         else
  335.         {
  336.             fm = getFontMetrics(tempFont);
  337.         }
  338.         
  339.         sLabelButton = sText;
  340.         textAscent = fm.getAscent();
  341.         textHeight = fm.getHeight();
  342.         textWidth  = fm.stringWidth(sLabelButton);
  343.     }
  344.  
  345.     /**
  346.      * Gets the current label text.
  347.      * @return the current label text
  348.      * @see #setText
  349.      */
  350.     public String getText()
  351.     {
  352.         return sLabelButton;
  353.     }
  354.  
  355.     /**
  356.      * Sets the label text color.
  357.      * @param color the new color for the label text
  358.      * @see #getTextColor
  359.      */
  360.     public void setTextColor(Color color)
  361.     {
  362.         textColor = color;
  363.     }
  364.  
  365.     /**
  366.      * Gets the current label text color.
  367.      * @return the current color of the label text
  368.      * @see #setTextColor
  369.      */
  370.     public Color getTextColor()
  371.     {
  372.         return textColor;
  373.     }
  374.  
  375.     /**
  376.      * Sets whether the label will be shown.
  377.      * @param f true to show the label, false otherwise
  378.      * @see #getShowLabel
  379.      */
  380.     public void setShowLabel(boolean f)
  381.     {
  382.         showLabel = f;
  383.     }
  384.  
  385.     /**
  386.      * Gets whether the label will be shown.
  387.      * @return true if label is shown, false otherwise
  388.      * @see #setShowLabel
  389.      */
  390.     public boolean getShowLabel()
  391.     {
  392.         return showLabel;
  393.     }
  394.  
  395.  
  396.     //--------------------------------------------------
  397.     // event methods
  398.     //--------------------------------------------------
  399.  
  400.  
  401.     //--------------------------------------------------
  402.     // class methods
  403.     //--------------------------------------------------
  404.  
  405.  
  406.     //--------------------------------------------------
  407.     // member methods
  408.     //--------------------------------------------------
  409.  
  410.     /**
  411.      * Paints this component using the given graphics context.
  412.      * This is a standard Java AWT method which typically gets called
  413.      * by the AWT to handle painting this component. It paints this component
  414.      * using the given graphics context. The graphics context clipping region
  415.      * is set to the bounding rectangle of this component and its <0,0>
  416.      * coordinate is this component's top-left corner.
  417.      * 
  418.      * @param g the graphics context used for painting
  419.      * @see java.awt.Component#repaint
  420.      * @see #update
  421.      */
  422.     public void paint(Graphics g)
  423.     {
  424.         Dimension s;
  425.         s = size();
  426.         
  427.         Rectangle r;
  428.         r = bounds();
  429.         
  430.         yTemp = r.height/2 + textAscent/2;
  431.         
  432.         switch(alignStyle)
  433.         {
  434.             case ALIGN_LEFT:
  435.             {
  436.                 if (bevelStyle == BEVEL_LINE)
  437.                 {
  438.                     xTemp = 4;
  439.                 }
  440.                 else
  441.                 {
  442.                     xTemp = 8;
  443.                 }
  444.  
  445.                 break;
  446.             }
  447.             case ALIGN_RIGHT:
  448.             {
  449.                 xTemp = r.width - textWidth;
  450.  
  451.                 if (bevelStyle == BEVEL_LINE)
  452.                 {
  453.                     xTemp -= 6;
  454.                 }
  455.                 else
  456.                 {
  457.                     xTemp -= 10;
  458.                 }
  459.  
  460.                 break;
  461.             }
  462.             case ALIGN_CENTERED:
  463.             {
  464.                 xTemp = (r.width - textWidth) / 2;
  465.                 break;
  466.             }
  467.         }
  468.         
  469.         switch(bevelStyle)
  470.         {
  471.             case BEVEL_RAISED:
  472.             {
  473.                 super.paint(g);
  474.                 if (showLabel)
  475.                 {
  476.                     g.setColor(textColor);
  477.                     if(pressed)
  478.                     {
  479.                         g.drawString(sLabelButton, xTemp + pressedAdjustment, yTemp + pressedAdjustment);
  480.                     }
  481.                     else
  482.                     {
  483.                          g.drawString(sLabelButton, xTemp, yTemp);
  484.                     }
  485.                 }
  486.             }
  487.             break;
  488.             case BEVEL_LOWERED:
  489.             {
  490.                 pressed = !pressed;
  491.                 super.paint(g);
  492.                 pressed = !pressed;
  493.                 
  494.                 if (showLabel)
  495.                 {
  496.                     g.setColor(textColor);
  497.                     if(pressed)
  498.                     {
  499.                         g.drawString(sLabelButton, xTemp - pressedAdjustment, yTemp - pressedAdjustment);
  500.                     }
  501.                     else
  502.                     {
  503.                          g.drawString(sLabelButton, xTemp, yTemp);
  504.                     }
  505.                 }
  506.             }
  507.             break;
  508.             case BEVEL_LINE:
  509.             {
  510.                 g.setColor(getForeground());
  511.                 g.drawRect(0 + indent, 0 + indent, s.width - 1 - indent - indent, s.height - 1 - indent - indent);
  512.             }
  513.             case BEVEL_NONE:
  514.             {
  515.                 if (showLabel)
  516.                 {
  517.                     g.setColor(textColor);
  518.                     g.drawString(sLabelButton, xTemp, yTemp);
  519.                 }
  520.  
  521.                 if(showInfoTip)
  522.                 {
  523.                     if (doInfoTip)
  524.                     {
  525.                         drawInfoTip();
  526.                     }
  527.                 }
  528.             }
  529.         }
  530.     }
  531.  
  532.     /**
  533.      * Sets this component's text font.
  534.      * This is a standard Java AWT method which gets called to change
  535.      * the font used for drawing text in this component.
  536.      *
  537.      * @param f the new font to use for drawing text
  538.      * @see java.awt.Component#getFont
  539.      */
  540.     public void setFont(Font f)
  541.     {
  542.         super.setFont(f);
  543.         fm = getFontMetrics(getFont());
  544.         textAscent    = fm.getAscent();
  545.         textHeight    = fm.getHeight();
  546.         textWidth    = fm.stringWidth(sLabelButton);
  547.     }
  548.     
  549.     /**
  550.      * Returns the recommended dimensions to properly display this component.
  551.      * This is a standard Java AWT method which gets called to determine
  552.      * the recommended size of this component. 
  553.      *
  554.      * @see #minimumSize
  555.      */
  556.     public Dimension preferredSize()
  557.     {
  558.         Dimension s = size();
  559.         Dimension m = minimumSize();
  560.  
  561.         return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
  562.     }
  563.  
  564.     /**
  565.      * Returns the minimum dimensions to properly display this component.
  566.      * This is a standard Java AWT method which gets called to determine
  567.      * the minimum size of this component. 
  568.      * 
  569.      * @see #preferredSize
  570.      */
  571.     public Dimension minimumSize()
  572.     {
  573.         Dimension min;
  574.         Font f;
  575.  
  576.         min = new Dimension(18, 10);
  577.         f   = getFont();
  578.  
  579.         if (f == null)
  580.         {
  581.             if (bOsFlag)
  582.             {
  583.                 min.height = 29;
  584.             }
  585.         }
  586.         else
  587.         {
  588.             min.width = textWidth + 18;
  589.             min.height = textHeight + 10;
  590.  
  591.             if (bOsFlag && min.height < 29)
  592.             {
  593.                 min.height = 29;
  594.             }
  595.         }
  596.  
  597.         return min;
  598.     }
  599.  
  600.     /**
  601.      * Moves and/or resizes this component.
  602.      * This is a standard Java AWT method which gets called to move and/or 
  603.      * resize this component. Components that are in containers with layout
  604.      * managers should not call this method, but rely on the layout manager
  605.      * instead.
  606.      * 
  607.      * @param x horizontal position in the parent's coordinate space
  608.      * @param y vertical position in the parent's coordinate space
  609.      * @param width the new width
  610.      * @param height the new height
  611.      */
  612.     public synchronized void reshape(int x, int y, int width, int height)
  613.     {
  614.         super.reshape(x, y, width, height);
  615.  
  616.         if (!isValid())
  617.         {
  618.             repaint();
  619.         }
  620.     }
  621.  
  622.     /**
  623.      * Handles redrawing of this component on the screen.
  624.      * This is a standard Java AWT method which gets called by the Java
  625.      * AWT (repaint()) to handle repainting this component on the screen.
  626.      * The graphics context clipping region is set to the bounding rectangle
  627.      * of this component and its <0,0> coordinate is this component's 
  628.      * top-left corner.
  629.      * Typically this method paints the background color to clear the
  630.      * component's drawing space, sets graphics context to be the foreground
  631.      * color, and then calls paint() to draw the component.
  632.      *
  633.      * It is overridden here to eliminate the unneeded repainting of the background.
  634.      *
  635.      * @param g the graphics context
  636.      * @see java.awt.Component#repaint
  637.      * @see #paint
  638.      */
  639.     public void update(Graphics g)
  640.     {
  641.         paint(g);
  642.     }
  643. }
  644.